package com.wxq.workpad.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.wxq.workpad.R
import com.wxq.workpad.entity.TopTitleEntity

/**
 * @Description: 类作用描述
 */
class TopTitleAdapter(context: Context):RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    private var mContext:Context
    private var mHasSize:Int=0
    private var mData:MutableList<TopTitleEntity>
    private lateinit var listener:(position:Int)->Unit

    init {
        mContext=context
        mData= mutableListOf()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return ItemHold(LayoutInflater.from(mContext).inflate(R.layout.item_top_title,parent,false))
    }

    override fun getItemCount(): Int {
        return mData.size
    }

    fun updateDate(data:MutableList<TopTitleEntity>){
        mData.clear()
        mData.addAll(data)
        notifyDataSetChanged()
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is ItemHold){
            if (mData.get(position).isSelect) {
                holder.tv_title_item.setBackgroundColor(mContext.resources.getColor(R.color.color_f8c104))
                holder.tv_title_item.setTextColor(mContext.resources.getColor(R.color.white))
            }else{
                holder.tv_title_item.setBackgroundColor(mContext.resources.getColor(R.color.color_a2a2a2))
                holder.tv_title_item.setTextColor(mContext.resources.getColor(R.color.color_595e6e))
            }

            holder.tv_title_item.setText( mData.get(position).title)

            holder.tv_title_item.setOnClickListener {
                for (mDatum in mData) {
                    mDatum.isSelect=false
                }
                mData.get(position).isSelect=true
                notifyDataSetChanged()
                if (listener!=null) {
                    listener(position)
                }
            }
        }
    }


     public  fun changeSelect(position:Int){
         for (mDatum in mData) {
             mDatum.isSelect=false
         }
         mData.get(position).isSelect=true
         notifyDataSetChanged()
     }


    fun setClickListener(itemClick:(position:Int)->Unit){
        listener=itemClick
    }


    inner class ItemHold(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var tv_title_item:TextView

        init {
            tv_title_item=itemView.findViewById(R.id.tv_title_item)

        }
    }
}